package be.bonamis.umpire.ui.client.mvp.impl;

import java.util.List;

import be.bonamis.projects.umpire.business.dto.UmpireDTO;
import be.bonamis.umpire.ui.client.mvp.BootView;
import be.bonamis.umpire.ui.client.resources.Resources;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.inject.Inject;

public class BootViewImpl extends Composite implements BootView {
	private UmpireDTO umpire;
	private Presenter presenter;
	
	interface BootViewImplUiBinder extends UiBinder<HTMLPanel, BootViewImpl> {
	}
	
	@UiField
	Anchor dispoButton;
	
	@UiField
	Anchor whoButton;
	
	@UiField
	Anchor quizButton;
	
	@UiField
	Anchor historyButton;
	
	@UiField
	Label fullName;
	
	@UiField
	SimplePanel simplePanel;
	
	@UiHandler("dispoButton")
	public void onClickDispo(ClickEvent event) {
		presenter.dispoButtonClicked();
	}
	
	@UiHandler("whoButton")
	public void onClickWho(ClickEvent event) {
		presenter.whoButtonClicked();
	}
	
	@UiHandler("quizButton")
	public void onClickQuiz(ClickEvent event) {
		presenter.quizButtonClicked();
	}
	
	@UiHandler("historyButton")
	public void onClickHistory(ClickEvent event) {
		presenter.historyButtonClicked();
	}

	@Inject
	public BootViewImpl(BootViewImplUiBinder uiBinder, final Resources resources) {			
		initWidget(uiBinder.createAndBindUi(this));
		postInit();
//		clubSuggestBox.addKeyUpHandler(new KeyUpHandler() {
//			@Override
//			public void onKeyUp(KeyUpEvent event) {
//				if (KeyCodes.KEY_ENTER != event.getNativeKeyCode()) {
//					if (clubSuggestBox.getText().length() >= 2) {
//						presenter.searchClubs(clubSuggestBox.getText());
//					}
//				}
//			}
//		});
	}
	
	private void postInit() {
		String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
		if (currentLocale.equals("default")) {
			currentLocale = "fr";
		}
		String[] localeNames = LocaleInfo.getAvailableLocaleNames();
		for (String localeName : localeNames) {
			if (!localeName.equals("default")) {
			}
		}
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public Presenter getPresenter() {
		return presenter;
	}

	@Override
	public void setValues(UmpireDTO dto) {
		this.umpire = dto;
		fullName.setText(umpire.getFirstName() +" " + umpire.getLastName());
		if (umpire.getImageData() != null) {
			Image image = new Image(umpire.getImageData());
			image.setHeight("250px");
			simplePanel.clear();
			simplePanel.add(image);
			simplePanel.setVisible(true);
		} else {
			simplePanel.setVisible(false);
		}
	}
	
	@Override
	public void updateClubList(List<String> clubsList) {
//		clubSuggestions.clear();
//		for (String club : clubsList) {
//			clubSuggestions.add(club);
//		}
		
	}	

	@Override
	public UmpireDTO getUmpire() {		
		return umpire;
	}	

}
